<?php

class UserMapper extends AbstractMapper
{

    protected function _init()
    {
	$this->setModel(new UserModel());
	$this->setTableName(USER_TABLE);
    }

    public function getRecoveryCodeByUserId($userId)
    {
	if (!$userId) {
	    return false;
	}
	return $this->createSqlCommand()
			->select("code")
			->from(USER_RECOVER_TABLE)
			->where("user_id = :id", array(":id" => (int) $userId))
			->queryScalar();
    }

    public function addRecoveryCode($code, $userId)
    {
	if (!$userId || !$code) {
	    return false;
	}
	$data = array('user_id' => $userId, 'code' => $code);
	if ($this->createSqlCommand()->insert(USER_RECOVER_TABLE, $data)) {
	    return true;
	}
	return false;
    }

    public function getUserByRecoveryCode($code)
    {
	if (!$code) {
	    return false;
	}
	return $this->createSqlCommand()
			->select("user_id")
			->from(USER_RECOVER_TABLE)
			->where("code = :code", array(":code" => $code))
			->queryScalar();
    }

}
